package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;

import com.example.demo.entity.ACM;
import com.example.demo.entity.Game;
import com.example.demo.entity.ItemBank;
import com.example.demo.entity.Sojob;
import com.example.demo.entity.result.NameValue;
import com.example.demo.mapper.GameMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/game")
public class GameController {

    @Resource
    GameMapper gameMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody Game game) {
        gameMapper.insert(game);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        gameMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查最原始的数据
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "4200") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Game> wrapper = Wrappers.<Game>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Game::getTitle,search);
        }
        Page<Game> gamePage = gameMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(gamePage);
    }

    @GetMapping("/ratio") //获得按难易排行的知识点
    public Result<?> GetRatioSort() {

        QueryWrapper queryWrapper = new QueryWrapper();
        QueryWrapper queryWrapper2 = new QueryWrapper();

        queryWrapper.select("DISTINCT language");
        List<Game> gameList = gameMapper.selectList(queryWrapper);

        List<Game> games = gameMapper.selectList(queryWrapper2);
        List<Map<String,Object>> ratioMap = new ArrayList<>();
        for(int i=0;i<gameList.size();i++) {
            String language = gameList.get(i).getLanguage();
            int t=0;
            System.out.println(language);
            for (int j = 0; j < games.size(); j++) {
                if(games.get(j).getLanguage().equals(language)){
                    t++;
                }

            }
            Map<String,Object> map = new HashMap<>();
            map.put("language",language);
            float ratio = (float)t / (float)games.size();
            map.put("ratio",ratio);
            ratioMap.add(map);
        }


        return Result.success(ratioMap);//各种语言实现的游戏占所有资源的比率

    }

    @GetMapping("/downloadNum") //统计不同语言的游戏下资源载量
    public Result<?> calNum(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "2000") Integer pageSize,
                             @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Game> wrapper = Wrappers.<Game>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Game::getLanguage,search);
        }
        Page<Game> sojobPage = gameMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        List<Game> records = sojobPage.getRecords();
        Map<String,Integer> mp = new HashMap<>();
        List<NameValue> nv=new ArrayList<>();

        for (Game record : records) {
            String language = record.getLanguage();
            Integer downloadNum = Integer.parseInt(record.getDownload());
                if (!mp.containsKey(language)) {
                    mp.put(language, downloadNum);
                } else {
                    mp.put(language, mp.get(language) + downloadNum);
                }
        }

        for(Map.Entry<String, Integer> entry : mp.entrySet()){
//            System.out.println("foreachEntry : key :" + entry.getKey() + "---> value :"+entry.getValue());
            nv.add(new NameValue(entry.getKey(),entry.getValue()));
        }

//        Collections.sort(nv20);
//        Collections.reverse(nv20);

//        List<NameValue> res=nv20.subList(0,20); //最喜欢用的前20条
        Page<NameValue> nameValuePage = new Page();  // 创建NameValue类型的Page,前端需要该类型的数据
        nameValuePage.setRecords(nv);
        nameValuePage.setTotal(sojobPage.getTotal());
        nameValuePage.setSize(sojobPage.getSize());
        nameValuePage.setCurrent(sojobPage.getCurrent());
        nameValuePage.setOrders(sojobPage.getOrders());
        nameValuePage.setPages(sojobPage.getPages());

        return Result.success(nameValuePage);
    }



    @GetMapping("/sort")
    public Result<?> GetSort(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "100") Integer pageSize,
                                  @RequestParam(defaultValue = "",required = false) String language,
                                  @RequestParam(defaultValue = "",required = false) String sortWay) {

        LambdaQueryWrapper<Game> wrapper = Wrappers.<Game>lambdaQuery();
        if(StrUtil.isNotBlank(language)) {//是否特指某一语言
            wrapper.like(Game::getLanguage,language);
        }
        if(sortWay.equals("title")){//排序的条件
            wrapper.orderByDesc(Game::getTitle);
        }
        else if(sortWay.equals("size")){
            wrapper.orderByDesc(Game::getSize);
        }
        else if(sortWay.equals("download")){
            wrapper.orderByDesc(Game::getDownload);
        }
        else {
            wrapper.orderByDesc(Game::getId);
        }
        Page<Game> gamePage = gameMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(gamePage);//
    }



    @PutMapping //改
    public Result<?> update(@RequestBody Game game) {
        gameMapper.updateById(game);
        return Result.success();
    }
}


